package com.htfyun.ft.fragment.factory;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.htfyun.ft.R;
import com.htfyun.ft.rooboUtils.RooboEngine;
import com.htfyun.ft.rooboUtils.XunFeiWakeUpTranslator;

public class FragmentWakeup extends FactoryBaseFragment implements View.OnClickListener {

    //wakeup
    private TextView txtWakeupTips;
    private TextView txtWakeupCount;
    private TextView txtWakeupCountDown;
    private TextView txtWakeupStatus;
    private Button btnWakeupTestStart;
    private static final int WAKE_UP_TIME_SECOND = 20;
    private static final int SUCCESS_WAKEUP_COUNT = 3;

    private RooboEngine rooboEngine;

    private int wakeupCount = 0;
    private int wakeupCountDown = WAKE_UP_TIME_SECOND;


    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_wakeup;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);

        txtWakeupTips = (TextView) findViewById(R.id.txtWakeupTips);
        txtWakeupCount = (TextView) findViewById(R.id.txtWakeupCount);
        txtWakeupCountDown = (TextView) findViewById(R.id.txtWakeupCountDown);
        txtWakeupStatus = (TextView) findViewById(R.id.txtWakeupStatus);
        btnWakeupTestStart = (Button) findViewById(R.id.btnWakeupTestStart);
        btnWakeupTestStart.setOnClickListener(this);
        txtWakeupTips.setText(getString(R.string.test_wakeup_tips, WAKE_UP_TIME_SECOND));

        clearWakeupTxt();

        setBtnPassVisibility(View.GONE);

    }

    @Override
    protected void onVisible() {
        super.onVisible();
        destroyIflytekVoiceRecognition();
    }

    @Override
    public void onClick(View v) {

        if (v == btnWakeupTestStart) {
            startWakeupTest();
        }

    }
    //wakeup
    private void clearWakeupTxt() {
        txtWakeupCount.setText("");
        txtWakeupCountDown.setText("");
        txtWakeupStatus.setText("");
    }

    private void startWakeupTest() {

        if (!btnWakeupTestStart.isEnabled()) {
            // 已经开始唤醒测试了
            return;
        }
        btnWakeupTestStart.setEnabled(false);

        startRecognition();
        wakeupCount = 0;
        wakeupCountDown = WAKE_UP_TIME_SECOND;
        txtWakeupCount.setText(getString(R.string.wakeup_success_count, wakeupCount, (SUCCESS_WAKEUP_COUNT - wakeupCount)));
        updateCountdown();


    }

    private void stopWakeupTest() {
        stopRecognition();
        getHandler().removeMessages(MSG_WAKEUP_COUNT_DOWN);

        btnWakeupTestStart.setEnabled(true);
    }

    private void startRecognition() {
        initIflytekVoiceRecognition();
    }

    private void stopRecognition() {
        destroyIflytekVoiceRecognition();

    }

    private void initIflytekVoiceRecognition() {

        destroyIflytekVoiceRecognition();

        rooboEngine = new RooboEngine(mContext);
        rooboEngine.init();
        rooboEngine.setWakeupListener(new WakeupListener());

        txtWakeupStatus.setText(getString(R.string.wakeup_test_status, getString(R.string.wakeup_status_doing)));
    }

    private void destroyIflytekVoiceRecognition() {

        if (rooboEngine != null) {
            rooboEngine.deinit();
            rooboEngine = null;
        }
    }

    private void handleRecognition() {

        wakeupCount++;

        txtWakeupCount.setText(getString(R.string.wakeup_success_count, wakeupCount, (SUCCESS_WAKEUP_COUNT - wakeupCount)));

        if (wakeupCount >= SUCCESS_WAKEUP_COUNT) {
            txtWakeupStatus.setText(getString(R.string.wakeup_test_status, getString(R.string.test_result_pass)));

            stopWakeupTest();

            finishMeWithPassed(true);
        }
    }

    private void updateCountdown() {

        if (wakeupCountDown <= 0) {
            if (wakeupCount < SUCCESS_WAKEUP_COUNT) {
                txtWakeupStatus.setText(getString(R.string.wakeup_test_status, getString(R.string.test_result_fail)));
                stopWakeupTest();
                finishMeWithPassed(false);
            }

            return;
        }

        wakeupCountDown--;
        txtWakeupCountDown.setText(getString(R.string.wakeup_test_countdown, wakeupCountDown));

        getHandler().sendEmptyMessageDelayed(MSG_WAKEUP_COUNT_DOWN, PERIOD_WAKEUP_COUNT_DOWN);
    }


    private class WakeupListener implements XunFeiWakeUpTranslator.WakeupListener {
        @Override
        public void onWakeUp(String json) {

            handleRecognition();
        }
    }
    //wakeup end


    private static final int MSG_WAKEUP_COUNT_DOWN = 1000;
    private static final int PERIOD_WAKEUP_COUNT_DOWN = 1000;

    @Override
    protected void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case MSG_WAKEUP_COUNT_DOWN:
                updateCountdown();
                break;
        }
    }
}
